package com.wibo.common;

import com.wibo.account.R;
import com.wibo.account.WiboMainTabActivity;
import com.wibo.account.WiboTabActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;

public class NotificationHelper {
	private static final String TAG = "NotificationHelper";
	
	private int mNotificationId = 20;
	public static final int NOTIFICATION_COMMENT = 0;
	public static final int NOTIFICATION_TRANSMIT = 1;
	public static final int NOTIFICATION_FANS = 2;
	
	private int mNotificationType = -1;
	
	
	private Context mContext;
	private NotificationManager mNotificationManager;
	private Notification mNotification;
	private String mWeiboName = "";
	private long mCount = -1;
	public NotificationHelper(Context context, String name, int id, int type, long count){
		mContext = context;
		mNotificationType = type;
		mCount = count;
		mWeiboName = name;
		mNotificationId = id;
	}
	
	public void createNotification(long count, Service service){
		mCount = count;
		setSyncIconVisibility(true);

		if (mNotificationManager == null) {
			mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		
		String text = "";
		switch (mNotificationType) {
		case NOTIFICATION_COMMENT:
			text = String.valueOf(mCount) + mContext.getString(R.string.newComments);
			break;
		case NOTIFICATION_FANS:
			text = String.valueOf(mCount) + mContext.getString(R.string.newFans);
			break;
		case NOTIFICATION_TRANSMIT:
			text = String.valueOf(mCount) + mContext.getString(R.string.newTransmits);
			break;
		}
		// 实例化一个通知，并指定其图标和标题（在提示栏上显示）
		String title = mWeiboName;
		mNotification = new Notification(R.drawable.ic_popup_sina_weibo, 
				text, System
				.currentTimeMillis());

		// 通知界面用自己定义的界面来显示
		mNotification.contentView = new RemoteViews(mContext.getPackageName(),
				R.layout.notification);
		mNotification.contentView.setTextViewText(R.id.title, title);
		mNotification.contentView.setTextViewText(R.id.tip, text);
		mNotification.contentView.setImageViewResource(R.id.notifyicon,
				R.drawable.ic_popup_sina_weibo);

		mNotification.contentIntent = getCancelPendingIntent();

		// 保证该通知一直存在
		mNotification.flags |= Notification.FLAG_AUTO_CANCEL/* | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS*/;
		mNotification.defaults = Notification.DEFAULT_ALL;
		// 发出通知（其中第一个参数为通知标识符）
		//mNotificationManager.notify(NOTIFICATION_ID, mNotification);
		
		//service.startForeground(mNotificationId, mNotification);
		mNotificationManager.notify(mNotificationId, mNotification);
	}
	
	/**
	 * 更新进度,如果没有调用{@link NotificationHelper#createNotification()} 则会在该函数调用
	 * @param percent
	 */
	public void updateNotification(long count, Service service){
		if (mNotificationManager == null) {
			mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		if (mNotification == null) {
			createNotification(count, service);
		}
		if(count > 0){
			try{
				mCount = count;
				String text = "";
				switch (mNotificationType) {
				case NOTIFICATION_COMMENT:
					text = String.valueOf(mCount) + mContext.getString(R.string.newComments);
					break;
				case NOTIFICATION_FANS:
					text = String.valueOf(mCount) + mContext.getString(R.string.newFans);
					break;
				case NOTIFICATION_TRANSMIT:
					text = String.valueOf(mCount) + mContext.getString(R.string.newTransmits);
					break;
				}
				
				mNotification.contentView.setTextViewText(R.id.tip, text);
				mNotificationManager.notify(mNotificationId, mNotification);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public void setSyncIconVisibility(boolean bVisible){
//		StatusBarManager statusbarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
//		statusbarManager.setIconVisibility("sync_active", bVisible);
	}
	
	// 取消窗口
	private PendingIntent getCancelPendingIntent(){
				
		Intent intent = new Intent(mContext, WiboMainTabActivity.class);
//		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//此处是为了只启动一个Activity
//        intent.addFlags(Intent.FILL_IN_DATA);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Bundle bundle = new Bundle();
		bundle.putInt("message", mNotificationType);
		bundle.putInt("notificationId", mNotificationId);
		intent.putExtras(bundle);
		return PendingIntent.getActivity(mContext, mNotificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	}
	
	public void cancelNotification(Service service){
		setSyncIconVisibility(false);

		if (mNotificationManager == null) {
			mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		//service.stopForeground(true);
		mNotificationManager.cancel(mNotificationId);
		mNotification = null;
	}
	

}
